package com.orm.analysis.service.impl;

import com.orm.analysis.dao.impl.AddressDAO;
import com.orm.analysis.dao.impl.ContactInfoDAO;
import com.orm.analysis.model.Address;
import com.orm.analysis.model.ContactInfo;
import com.orm.analysis.service.GenericService;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/10/13
 * Time: 11:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class ContactInfoService  implements GenericService<ContactInfo> {


    private static Logger log = Logger.getLogger(ContactInfoService.class);

    private String contactInfoDataFielDir;

    private ContactInfoDAO dao;

    private AddressDAO addressDAO;



    public AddressDAO getAddressDAO() {
        return addressDAO;
    }

    public void setAddressDAO(AddressDAO addressDAO) {
        this.addressDAO = addressDAO;
    }

    public String getContactInfoDataFielDir() {
        return contactInfoDataFielDir;
    }

    public void setContactInfoDataFielDir(String contactInfoDataFielDir) {
        this.contactInfoDataFielDir = contactInfoDataFielDir;
    }

    public ContactInfoDAO getDao() {
        return dao;
    }

    public void setDao(ContactInfoDAO dao) {
        this.dao = dao;
    }

    public ContactInfo create(ContactInfo entity) {
        dao.save(entity);
        return entity;
    }


    public void delete(Object id) {

        dao.delete(id);
    }

    public ContactInfo update(ContactInfo entity) {
        dao.update(entity);
        return entity;
    }

    public List<ContactInfo> list(int page, int size) {

            return dao.list(page,size);
    }

    public List<ContactInfo> listByFields(String... fieldsNamesValues) {
        StringBuffer queryString= new StringBuffer("select a from ContactInfo a ");
        return dao.listByFields(queryString,fieldsNamesValues);
    }

    public List<ContactInfo> findAllWithDepth(String... fetchRelations) {
         return dao.findAllWithDepth(ContactInfo.class,fetchRelations);
     }

    public  int updateExecuteUpdateByQuery(String query){
        return dao.updateExecuteUpdateByQuery(query);
    }

     public ContactInfo findWithDepth(String pkFieldName, Object id, String... fetchRelations) {
         return dao.findWithDepth(ContactInfo.class,pkFieldName,id,fetchRelations);
     }


    public ContactInfo getById(ContactInfo entity) {
        return dao.getById(entity.getContactInfoId());
    }

    public ContactInfo getById(Integer id) {
        return dao.getById(id);
    }

    @Transactional()
    public void executeImproter() throws ParseException,IOException {
        try{
        log.info("File  >>" + contactInfoDataFielDir);
        BufferedReader br = null;

        br = new BufferedReader(new FileReader(contactInfoDataFielDir));
        String line = br.readLine();
        line = br.readLine();//Skip 1 line

        while (line != null && line.trim().length()>0) {
            log.info("File  >>" + line);
            //CONTACT_INFO_ID,CON_INFO_ID(Address~Address),EMAIL_ID,PHONE_NO
            ContactInfo contactInfo = new ContactInfo();
            String[] data = line.split("[,]");


            contactInfo.setEmailId(data[1]);
            contactInfo.setPhoneNo(data[2]);

            dao.save(contactInfo);

//
            if (data[0] != null && data[0].length() > 0) {
                List<Address> addressList = new ArrayList<Address>();
                if (data[0].contains("~")) {
                    String[] addressIdArray = data[0].split("[~]");
                    for (int i = 0; i < addressIdArray.length; i++) {

                        if (addressIdArray[i].trim().length() > 0) {
                            addressList.add(addressDAO.getById(Integer.valueOf(addressIdArray[i])));

//                                        addressList.add(new Address(Integer.valueOf(addressIdArray[i].trim())));

                        }

                    }
                } else {
                    if (data[0].trim().length() > 0) {
                        addressList.add(addressDAO.getById(Integer.valueOf(data[0].trim())));
//                                    addressList.add(new Address(Integer.valueOf(data[0].trim())));

                    }
                }

                if (addressList.size() > 0) {
                    contactInfo.getAddresses().addAll(addressList);
                    System.out.print(">>>>" + addressList.size());
                }
            }
            dao.update(contactInfo);


            line = br.readLine();

        }

        br.close();
    }catch(Exception e ){
                e.printStackTrace();
            }
    }

    public List<Integer> getIdList() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void processTaskExecutor() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<ContactInfo> getByIdList(Integer[] entityIdList) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
